package com.jmb.apiplatform.util;


import com.jmb.apiplatform.exception.LoginException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.jmb.apiplatform.constant.Constants.SIGNIFY;

@Slf4j
@Component
public class TokenUtil {



    // 判断token是否到期
    public  boolean validateToken(String token, HttpServletRequest request) throws LoginException {
       try {
           Claims claims = Jwts.parser()
                .setSigningKey(SIGNIFY)
                .parseClaimsJws(token)
                .getBody();
           log.info("获取到的claims:{}",claims.toString());
           return !claims.getExpiration().before(new Date());
       } catch (Exception e)  {
           throw new LoginException("token过期");
       }
    }
}
